package com.da.sumup.controller;

import com.da.sumup.dao.bean.ArticleDO;
import com.da.sumup.pojo.*;
import com.da.sumup.service.IArticleService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/home")
public class HomeController {


    @Autowired
    private IArticleService articleService;

    @Autowired
    private ModelMapper modelMapper;

    @RequestMapping("/list")
    public ArticleTbVO list(PageBO pageBO) {
        ArticleTbVO articleTbVO = new ArticleTbVO();
        Page<ArticleDO> articleDOS = articleService.findAll(pageBO,new ArrayList<>());
        List<ArticleVO> articleVOS = articleDOS.stream().map(articleDO -> modelMapper.map(articleDO,ArticleVO.class)).collect(Collectors.toList());
        articleTbVO.setData(articleVOS);
        articleTbVO.setPage(pageBO.getPage());
        articleTbVO.setLimit(pageBO.getLimit());
        articleTbVO.setCount(articleDOS.getTotalElements());
        return articleTbVO;
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public BaseResult save(ArticleBO articleBO) {
        BaseResult baseResult = new BaseResult();
        articleService.save(articleBO);
        return baseResult;
    }

    @RequestMapping(value = "/delete/{uuid}",method = RequestMethod.DELETE)
    public BaseResult delete(@PathVariable("uuid") String uuid) {
        BaseResult baseResult = new BaseResult();
        articleService.delete(uuid);
        return baseResult;
    }

}
